package com.apes.fn.server.servicePlan.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.product.model.Category;
import com.apes.scm.masterdata.product.repository.CategoryRepository;
import com.apes.scm.masterdata.product.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 类描述：轮胎询价
 *
 * @ClassName TyreEnquiryService
 * @Description 描述
 * @Author wushilong
 * @Date 2023/7/13 15:14
 * @Version 1.0
 */

@Service("tyreEnquiryService")
public class TyreEnquiryService extends DomainService {

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    public Map tyreSpecification(SimpleRequest request){
        List models = invoke("dc.tyreProduct.findDistinctSpecification", request.getJO());
        return MapUtil.mapper("content", models, "size", models.size(), "page", 0);
    }

    public JSONArray getTyreItem(JSONArray tyreArray){
        if (Objects.isNull(tyreArray) || tyreArray.isEmpty())return null;
        for (int i = 0; i < tyreArray.size(); i++) {
            JSONObject parts = tyreArray.getJSONObject(i);

            JSONArray pictures = new JSONArray();
            if (parts.containsKey("partsInfos")){
                parts.put("note", parts.getJSONObject("partsInfos").getString("otherRemark"));
                parts.put("price", parts.getJSONObject("partsInfos").getString("price"));
                pictures = parts.getJSONObject("partsInfos").getJSONArray("otherImg");
                parts.put("model", parts.getJSONObject("partsInfos").getString("model"));
            }else {
                parts.put("note", parts.getString("note"));
                parts.put("price", parts.getString("price"));
                pictures = parts.getJSONArray("oldPicture");
                parts.put("model", parts.getString("oem"));
                parts.put("partsCodes", parts.getString("oem"));
            }
            parts.put("saleQty", 1);
            parts.put("oldPicture", pictures);
            parts.put("productId", parts.getString("productId"));
            Category tyre = categoryRepository.findOne("000609");
            parts.put("category", tyre);
            parts.put("id",i);
        }
        return tyreArray;
    }
}
